20. Calculating NB Accuracy

Calculating NB Accuracy

Question:

Start Quiz:

def NBAccuracy(features_train, labels_train, features_test, labels_test):
    """ compute the accuracy of your Naive Bayes classifier """
    ### import the sklearn module for GaussianNB
    from sklearn.naive_bayes import GaussianNB

    ### create classifier
    clf = #TODO

    ### fit the classifier on the training features and labels
    #TODO

    ### use the trained classifier to predict labels for the test features
    pred = #TODO


    ### calculate and return the accuracy on the test data
    ### this is slightly different than the example, 
    ### where we just print the accuracy
    ### you might need to import an sklearn module
    accuracy = #TODO
    return accuracy
from class_vis import prettyPicture
from prep_terrain_data import makeTerrainData
from classify import NBAccuracy

import matplotlib.pyplot as plt
import numpy as np
import pylab as pl


features_train, labels_train, features_test, labels_test = makeTerrainData()

def submitAccuracy():
    accuracy = NBAccuracy(features_train, labels_train, features_test, labels_test)
    return accuracy
Solution:

INSTRUCTOR NOTE:

In this example, we print the accuracy. However, the quiz is slightly different in that you return the accuracy, rather than printing it directly.

Remember, accuracy is defined as the number of test points that are classified correctly divided by the total number of test points.

There's another way you can do this, too –

print clf.score(features_test, labels_test)